
local Data "CountyYearFirmSizes"

noi disp "Obtaining national employment growth by firm-size size data..."
noi disp "Verifying that `Data' does not already exist..."
* This returns an error if the file does not exist.
capture confirm file "`Data'.dta"

* If an error is returned, then the commands below are run.
if _rc == 601 {
	noi disp "Data not found.  Building data..."
	noi disp "Loading US_county_firmsize_total.csv..."
	noi etime
	insheet using "$SourceData\US_county_firmsize_total.csv", clear
	noi disp "Finished loading."
	noi etime

	
	*Use total county by firm size less financial by firm size
	*geography is county

	* Verify that there are no duplicates
	isid geography year firmsize industry
		
	gen emp_size = emp
	label var emp "Total Employment"

	keep geography year firmsize emp_size

	/* Combining Lowest Three Employment Categories into one */
	gen Firm123 = emp_size if firmsize==1 | firmsize==2 | firmsize==3
	* Finds total of all firms of size 1, 2, and 3
	bysort year geography: egen TotalFirm123 = sum(Firm123)
	drop Firm123 
	* Replace observation from firm size 1 with total of firms 1, 2, and 3
	replace emp_size = TotalFirm123 if firmsize==1
	drop TotalFirm123 
	* Keep only two sizes: Firms 1,2,3 and Firms 5. Keep total employment for shares.
	keep if firmsize==1 | firmsize==5 | firmsize==0

	* Drop if emp_size is missing
	* Note: These missings are not coded as zeros
	drop if missing(emp_size)

	* Keep only if the first year observed for each county is 2000.
	bysort geography: egen minyear = min(year)
	keep if minyear==2000
	drop minyear
	* Keep only if the last year observed for each county is 2017.
	bysort geography: egen maxyear = max(year)
	keep if maxyear==2017
	drop maxyear
	* Note that there is a large firm and small firm observation for each county-year).

	noi disp "Reshaping data where i(geography year) and j(firmsize)."
	reshape wide emp, i(geography year) j(firmsize)
	
	

*Require Observations of Large and Small Firm sizes for each county-year
noi drop if missing(emp_size5) | missing(emp_size1)
noi disp "11,401 observations deleted?"

*Check unique geography-years
isid geography year
	
*County shares by firmsize
egen tot_emp = rowtotal(emp_size1 emp_size5)
gen emp_size_share1 = emp_size1/emp_size0
gen emp_size_share5 = emp_size5/emp_size0

xtset geography year
	
*Specify period over which shocks occur
foreach span in 1 3 5 15 {
	*Construct Bartik Instrument
	gen gLargeFirm`span' 		= log(emp_size5+1) - log(L`span'.emp_size5+1) 
	gen gSmallFirm`span' 		= log(emp_size1+1) - log(L`span'.emp_size1+1)
	gen dLargeShare`span' 		= emp_size_share5 - L`span'.emp_size_share5
	gen dSmallShare`span' 		= emp_size_share1 - L`span'.emp_size_share1
}

	noi disp "Saving `Data'.dta"
	compress
	save "$LocalData\\`Data'.dta", replace
	save "$LocalData\Archive\\`Data'`CurrentDate'.dta", replace
}
* This runs if no error was returned.
else noi disp "Data already exists."
noi etime
noi disp " "
